package com.seventh.parkingrent.controller;


import com.seventh.parkingrent.commens.Result;
import com.seventh.parkingrent.controller.form.UserAuditForm;
import com.seventh.parkingrent.controller.form.UserAuditFormForFront;
import com.seventh.parkingrent.domain.User;
import com.seventh.parkingrent.exception.CustomException;
import com.seventh.parkingrent.service.IUserAuditService;
import com.seventh.parkingrent.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户审核记录表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@RestController
@CrossOrigin("*")
@Slf4j
public class UserAuditController {
    @Resource
    private IUserAuditService userAuditService;
    @Resource
    private IUserService userService;

    @GetMapping("/getUserAuditByCondition")
    public Result getUserAuditByCondition(String managerName, String username, String beginTime,String endTime) throws ParseException {
        log.info("获得前端查询用户审核记录数据managerName={},username={},beginTime={},endTime={}",managerName,username,beginTime,endTime);
        if(managerName==""){
            managerName=null;
        }
        if(username==""){
            username=null;
        }
        List<UserAuditFormForFront>  form = userAuditService.getUserAuditByCondition(managerName,username,beginTime,endTime);

        return Result.success(form);
    }

    @PostMapping("/confirmAuditUser")
    @Transactional
    public Result confirmAuditUser(@Valid @RequestBody UserAuditForm userAuditForm, BindingResult bindingResult){
        log.info("获取前端用户审核意见表userAuditForm={}",userAuditForm);
        if(bindingResult.hasErrors()){
            return getBindingResult(bindingResult);
        }
        //添加用户审核记录
        Integer addUserAuditResult = userAuditService.addUserAuditResult(userAuditForm);
        if(0==addUserAuditResult){
            throw new CustomException("用户状态审核在后端添加中发生错误");
        }

        //修改用户表中状态
        @NotNull Integer userId = userAuditForm.getUserId();
        @NotBlank String isaudit = userAuditForm.getIsaudit();
        Integer updateResult = userService.updateUserAudit(userId, isaudit);
        if(0==updateResult){
            throw new CustomException("用户状态在后端修改中发生错误");
        }
        return Result.success();
    }

    public Result getBindingResult(BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        StringBuilder stringBuilder = new StringBuilder();
        for (FieldError fieldError : fieldErrors) {
            stringBuilder.append(fieldError.getField()+"："+fieldError.getDefaultMessage()+";");
        }
        return Result.fail("10086",stringBuilder.toString());
    }
}

